package github.leavesczy.reactivehttpsamples.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.InputType;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.SizeUtils;

import github.leavesczy.reactivehttpsamples.R;

public class BridgeEditButtonItem extends LinearLayout {
    private  EditText editTextView;
    private TextView buttonTextView;

    private boolean rightTextEnable;
    public BridgeEditButtonItem(Context context) {
        this(context, null);
    }

    public BridgeEditButtonItem(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BridgeEditButtonItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        try (TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BridgeEditButtonItem)) {
            String leftText = ta.getString(R.styleable.BridgeEditButtonItem_bebLeftText);
            int leftTextColor = ta.getColor(R.styleable.BridgeEditButtonItem_bebLeftTextColor, Color.BLACK);
            float leftTextSize = ta.getDimension(R.styleable.BridgeEditButtonItem_bebLeftTextSize, 14);

            String rightTextHint = ta.getString(R.styleable.BridgeEditButtonItem_bebRightTextHint);
            int rightTextColor = ta.getColor(R.styleable.BridgeEditButtonItem_bebRightTextColor, Color.BLACK);
            float rightTextSize = ta.getDimension(R.styleable.BridgeEditButtonItem_bebRightTextSize, 14);
            int inputType = ta.getInt(R.styleable.BridgeEditButtonItem_bebRightInputType, 0);
            rightTextEnable = ta.getBoolean(R.styleable.BridgeEditButtonItem_bebRightTextEnable, true);

            String buttonText = ta.getString(R.styleable.BridgeEditButtonItem_bebButtonText);
            int buttonTextColor = ta.getColor(R.styleable.BridgeEditButtonItem_bebButtonTextColor, Color.BLACK);
            float buttonTextSize = ta.getDimension(R.styleable.BridgeEditButtonItem_bebButtonTextSize, 12);

            setOrientation(LinearLayout.HORIZONTAL);
            setGravity(Gravity.CENTER_VERTICAL);

            setWeightSum(5);

            //添加左侧 Text
            TextView leftTextView=new TextView(context);
            leftTextView.setText(leftText);
            leftTextView.setTextColor(leftTextColor);
            leftTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX,leftTextSize);
            LayoutParams leftParams=new LayoutParams(0,LayoutParams.MATCH_PARENT);
            leftParams.weight=2;
            leftParams.leftMargin= SizeUtils.dp2px(10);
            leftTextView.setGravity(Gravity.CENTER_VERTICAL);
            addView(leftTextView,leftParams);

            //添加右侧的 EditText 按钮
            FrameLayout rlRight=new FrameLayout(context);

            editTextView=new EditText(context);
            editTextView.setHint(rightTextHint);
            editTextView.setTextColor(rightTextColor);
            editTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX,rightTextSize);
            editTextView.setEnabled(rightTextEnable);
            setInputType(editTextView,inputType);
            editTextView.setLines(1);
            editTextView.setGravity(Gravity.CENTER_VERTICAL);
            FrameLayout.LayoutParams rightEdParams=new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT);
            rlRight.addView(editTextView,rightEdParams);

            buttonTextView=new TextView(context);
            buttonTextView.setTextColor(buttonTextColor);
            buttonTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX,buttonTextSize);
            buttonTextView.setText(buttonText);
            buttonTextView.setPadding(SizeUtils.dp2px(5),SizeUtils.dp2px(1),SizeUtils.dp2px(5),SizeUtils.dp2px(1));
            buttonTextView.setBackgroundResource(R.drawable.shape_edit_button_btn);
            FrameLayout.LayoutParams rightBtParams=new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT);
            rightBtParams.gravity=Gravity.RIGHT|Gravity.CENTER_VERTICAL;
            rlRight.addView(buttonTextView,rightBtParams);


            LayoutParams rightParams=new LayoutParams(0,LayoutParams.MATCH_PARENT);
            rightParams.weight=3;
            rightParams.rightMargin= SizeUtils.dp2px(10);

            addView(rlRight,rightParams);


            setBackgroundResource(R.drawable.shape_bridge_edit_item_bg);

        }
    }

    private void setInputType(EditText editText, int inputType) {
        if(inputType==0){
            editText.setInputType(InputType.TYPE_CLASS_TEXT);
        }else if(inputType==1){
            editText.setInputType(InputType.TYPE_CLASS_NUMBER);
        }else if(inputType==2){
            editText.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
        }
    }

    public void setEditTextEnable(boolean enabled){
        if(rightTextEnable){
            editTextView.setEnabled(enabled);
        }
        buttonTextView.setEnabled(enabled);
    }

    public String getEditTextStr(){
        return editTextView.getText().toString();
    }

    public void setEditTextStr(String textStr){
        editTextView.setText(textStr);
    }


    public void initButtonTextListener(View.OnClickListener l){
        buttonTextView.setOnClickListener(l);
    }

}
